package com.feedgraph;

import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;

import android.content.Context;
import android.content.res.AssetFileDescriptor;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Bitmap.CompressFormat;
import android.media.MediaMetadataRetriever;
import android.media.ThumbnailUtils;
import android.net.Uri;
import android.provider.MediaStore;
import android.util.Log;

public class Uploader {

	public static final String POST_URL = "http://upload.feedgraph.com/";
	
	protected Context mContext;
	
	public Uploader(Context context) {
		mContext = context;
	}
	
	/** returns http URI to uploaded image on server 
	 * 
	 * @param mFile
	 * @return
	 * @throws IOException 
	 */
	public Uri upload(Uri mFile, String mimeType) throws IOException {
		
		Log.d(getClass().getSimpleName (), "uploading raw data file at " + mFile.toString ());
		
		AssetFileDescriptor fd = mContext.getContentResolver ()
				.openAssetFileDescriptor (mFile, "r");
		
		HttpURLConnection conn = (HttpURLConnection) new URL(POST_URL).openConnection ();
		conn.setDoOutput(true);
    conn.setRequestProperty("Content-Type", mimeType);
    conn.setRequestMethod("POST");    

		OutputStream out = conn.getOutputStream ();
		IOUtil.copy (fd.createInputStream (), out);
		out.flush ();
		out.close ();

		if (conn.getResponseCode () != 200) {
			throw new IOException(conn.getResponseMessage ());
		}
		
		Log.d(getClass().getSimpleName (), "done");
		return Uri.parse (conn.getHeaderField ("X-Location"));
	}

	public Uri uploadBitmap(Uri mFile) throws FileNotFoundException, IOException {
		
		Log.d(getClass().getSimpleName (), "uploading Bitmap file at " + mFile.toString ());
		
		Bitmap bitmap = null;
		bitmap = MediaStore.Images.Media.getBitmap(mContext.getContentResolver(), mFile);
		float aspect = (float) bitmap.getHeight () / (float) bitmap.getWidth ();
		bitmap = Bitmap.createScaledBitmap (bitmap, 400, (int) (400 * aspect), false);
	
		HttpURLConnection conn = (HttpURLConnection) new URL(POST_URL).openConnection ();
		conn.setDoOutput(true);
    conn.setRequestProperty("Content-Type", "image/jpeg");
    conn.setRequestMethod("POST");    

		OutputStream out = conn.getOutputStream ();
		bitmap.compress (CompressFormat.JPEG, 70, out);
		
		Log.d(getClass().getSimpleName (), "done");
		return Uri.parse (conn.getHeaderField ("X-Location"));
	}
	
	public Uri uploadVideoThumbnail(Uri mFile) throws MalformedURLException, IOException {
		
		Log.d(getClass().getSimpleName (), "uploading Video Thumbnail file at " + mFile.toString ());
		
  	Bitmap bitmap = ThumbnailUtils.createVideoThumbnail (mFile.getPath (), MediaStore.Images.Thumbnails.MICRO_KIND);
  	
  	if (bitmap == null) return null;

  	HttpURLConnection conn = (HttpURLConnection) new URL(POST_URL).openConnection ();
		conn.setDoOutput(true);
    conn.setRequestProperty("Content-Type", "image/jpeg");
    conn.setRequestMethod("POST");    

		OutputStream out = conn.getOutputStream ();
		bitmap.compress (CompressFormat.JPEG, 70, out);
		
		Log.d(getClass().getSimpleName (), "done");
		return Uri.parse (conn.getHeaderField ("X-Location"));
	}
	
	public Uri uploadAlbumArt(Uri mFile) throws MalformedURLException, IOException {
		
		Log.d(getClass().getSimpleName (), "uploading Album Art file at " + mFile.toString ());
		
		MediaMetadataRetriever metaDataRetriver = new MediaMetadataRetriever();
		metaDataRetriver.setDataSource(mContext, mFile);
		byte [] artwork = metaDataRetriver.getEmbeddedPicture();
		Bitmap bitmap = BitmapFactory.decodeByteArray(artwork, 0, artwork.length);
		
		HttpURLConnection conn = (HttpURLConnection) new URL(POST_URL).openConnection ();
		conn.setDoOutput(true);
    conn.setRequestProperty("Content-Type", "image/jpeg");
    conn.setRequestMethod("POST");    

		OutputStream out = conn.getOutputStream ();
		bitmap.compress (CompressFormat.JPEG, 70, out);
		
		Log.d(getClass().getSimpleName (), "done");
		
		return Uri.parse (conn.getHeaderField ("X-Location"));
	}
}
